/*
 * CsvReader.java
 * Copyright: Tsingsoft (c) 2015
 * Company: 鍖椾含娓呰蒋鍒涙柊绉戞妧鏈夐檺鍏徃
 */
package com.tsingsoft.csv;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.supercsv.io.CsvListReader;
import org.supercsv.prefs.CsvPreference;

import com.tsingsoft.csv.bean.CsvBean;

/**
 * 
 * <pre>Csv 鏂囦欢璇诲彇  <pre>
 * @author L.Yang
 * @version 1.0, 2015骞�鏈�9鏃�
 */
@Service("csvReader")
public class CsvReader {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	private CsvListReader reader;

	/**
	 * 鎵归噺璇诲彇 csv 鏂囦欢
	 * @param filePath 鏂囦欢鐩綍
	 * @return 杩斿洖csv鍒楄〃
	 */
	public List<CsvBean> readBatch(String filePath) {
		List<CsvBean> values = new ArrayList<CsvBean>();
		try {
			long before = System.currentTimeMillis();
			File floder = new File(filePath);
			File[] files = floder.listFiles();
			for(File file :files){
				if(!file.getName().toLowerCase().contains(".csv")) continue;
				InputStreamReader inputReader = new InputStreamReader(
						new FileInputStream(file));
				reader = new CsvListReader(inputReader, CsvPreference.STANDARD_PREFERENCE);
				List<List<String>> datas = new ArrayList<List<String>>();
				List<String> list = null;
				String[] headers = reader.getHeader(true);
				while ((list = reader.read()) != null) {
					datas.add(list);
				}
				CsvBean csv = new CsvBean();
				csv.setDatas(datas);
				csv.setHeaders(headers);
				csv.setName(file.getName());
				values.add(csv);
				
				logger.debug("瀹屾垚  {} 瑙ｆ瀽, 鑰楁椂 {} ms", new Object[] { file.getName(),
						(System.currentTimeMillis() - before) });
			}
		} catch (Exception e) {
			logger.error(ExceptionUtils.getFullStackTrace(e));
		} finally {
			close();
		}
		return values;
	}
	
	
	/**
	 * 璇诲彇鍗曚釜csv 鏂囦欢
	 * @param fileName csv鏂囦欢鍚嶇О
	 * @return csv 瀹炰綋
	 */
	public CsvBean read(String fileName) {
		CsvBean csv = new CsvBean();
		try {
			List<List<String>> datas = new ArrayList<List<String>>();
			File file = new File(fileName);
			long before = System.currentTimeMillis();
			InputStreamReader inputReader = new InputStreamReader(
					new FileInputStream(file));
			reader = new CsvListReader(inputReader, CsvPreference.STANDARD_PREFERENCE);
			String[] headers = reader.getHeader(true);
			List<String> list = null;
			while ((list = reader.read()) != null) {
				datas.add(list);
			}
			
			csv.setDatas(datas);
			csv.setHeaders(headers);
			csv.setName(file.getName());
			
			logger.debug("瀹屾垚  {} 瑙ｆ瀽, 鑰楁椂 {} ms", new Object[] { file.getName(),
					(System.currentTimeMillis() - before) });
		} catch (Exception e) {
			logger.error(ExceptionUtils.getFullStackTrace(e));
		} finally {
			close();
		}
		return csv;
	}

	/**
	 * 鍏抽棴 reader
	 */
	private void close() {
		try {
			reader.close();
		} catch (IOException e) {
			logger.error(ExceptionUtils.getFullStackTrace(e));
		}
	}
}
